﻿using Centny.PdfReader.Except;
using Centny.PdfReader.Font;
using Centny.PdfReader.Types;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;

namespace Centny.PdfReader.Pdf
{
    public class PdfPage : PdfClassObject
    {
        public PdfClassObject Pco { get; set; }
        private Rect _MediaBox;
        public Rect MediaBox
        {
            get
            {
                return this._MediaBox;
            }
        }
        private PdfDictionary _Resources;
        public PdfDictionary Resources
        {
            get
            {
                return this._Resources;
            }
        }
        public PdfPage(PdfClassObject pco)
        {
            this.Pco = pco;
            this.Doc = this.Pco.Doc;
            this.InitMediaBox();
            this.InitResources();
        }
        private void InitMediaBox()
        {
            Rect rt = new Rect();
            PdfArray ary = this.Pco.AsDictionary[PageKeys.MediaBox] as PdfArray;
            rt.X = (ary[0] as PdfDouble).Val;
            rt.Y = (ary[1] as PdfDouble).Val;
            rt.Width = (ary[2] as PdfDouble).Val;
            rt.Height = (ary[3] as PdfDouble).Val;
            this._MediaBox = rt;
        }
        private void InitResources()
        {
            PdfClass rcls = this.Pco.AsDictionary[PageKeys.Resources];
            if (rcls is PdfDictionary)
            {
                this._Resources = rcls as PdfDictionary;
            }
            else if (rcls is PdfReference)
            {
                PdfReference pref = rcls as PdfReference;
                PdfClassObject pco = this.Doc.RealObject(pref) as PdfClassObject;
                this._Resources = pco.AsDictionary;
            }
        }
        public PdfClassObject LoadFont(PdfName n)
        {
            PdfClass fobj = this.Resources[PageResourceKeys.Font];
            PdfDictionary fonts;
            if (fobj is PdfReference)
            {
                PdfClassObject obj = this.Doc.RealObject(fobj as PdfReference) as PdfClassObject;
                fonts = obj.AsDictionary;
            }
            else if (fobj is PdfDictionary)
            {
                fonts = fobj as PdfDictionary;
            }
            else
            {
                throw new PdfException("not fonts in page");
            }
            PdfReference pref = fonts[n] as PdfReference;
            if (pref == null)
            {
                return null;
            }
            else
            {
                return this.Doc.RealObject(pref) as PdfClassObject;
            }
        }
        public void Process()
        {
            PdfDictionary dict = this.Pco.InnerObj as PdfDictionary;
            PdfClass cl = dict[PageKeys.Contents];
            if (cl is PdfArray)
            {
                foreach (PdfReference pref in (cl as PdfArray).Objects)
                {
                    this.Process(pref);
                }
            }
            else if (cl is PdfReference)
            {
                this.Process(cl as PdfReference);
            }

        }
        private void Process(PdfReference pref)
        {
            PdfClassObject pco = this.Doc.RealObject(pref) as PdfClassObject;
            if (pco is PdfStreamObject)
            {
                this.Process(pco as PdfStreamObject);
            }
            else if (pco.AsArray != null)
            {
                foreach (PdfReference cp in pco.AsArray.Objects)
                {
                    this.Process(cp);
                }
            }
        }
        public virtual void Process(PdfStreamObject sobj)
        {

        }

    }
}
